###########################################################################
# Lipps & Sczepanski & Malet
# Understanding Preferences over Borders
# International Studies Quaterly

# Appendix: Analysis of East-West divides --------------------------------
###########################################################################

rm(list = ls())

packages <- c("tidyverse", "cregg", "tidytext", "ggplot2", "xtable")

for (package in packages) {
  if (!requireNamespace(package, quietly = TRUE)) {
    install.packages(package, dependencies = TRUE)
  }
}

library(tidyverse)
library(ggplot2)
library(cregg)
library(xtable)
library(viridis)
library(sjPlot)
library(texreg)
library(ggpubr)

#Load data
dat_long <- read.csv("survey_dat_clean_conjoint.csv", na.strings=c("","NA"))
dat <- read.csv("survey_dat_clean.csv", na.strings=c("","NA"), row.names = 1)

#recode
dat_long$border <- as.factor(dat_long$border)
dat_long$country <- as.factor(dat_long$country)
dat_long$aim <- as.factor(dat_long$aim)
dat_long$gender <- as.factor(dat_long$gender)
dat_long$migration_background <- as.factor(dat_long$migration_background)
dat_long <- dat_long %>% mutate(ironcurtaindist = as.factor(case_when(distance_ironcurtain<=50 ~ "<50km",
                                                                      distance_ironcurtain>50 & distance_ironcurtain<=100 ~ "<100km",
                                                                      distance_ironcurtain>100 & distance_ironcurtain<=150 ~ "<150km",
                                                                      distance_ironcurtain>150 ~ ">150km")))

dat_long$ironcurtaindist <- ordered(dat_long$ironcurtaindist, levels = c("<50km", "<100km", "<150km", ">150km"))


dat$age <- as.numeric(dat$age)
dat <- dat %>% mutate(age_cat = as.factor(case_when(age<25 ~ "18-24",
                                                    age>=25 & age<35 ~ "25-34",
                                                    age>=35 & age <45 ~ "35-44",
                                                    age>=45 & age<55 ~ "45-54",
                                                    age>=55 & age<64 ~ "55-64",
                                                    age>=65 ~ "65 or older")))

dat_long <- dat_long %>% left_join(dat[,c("id", "age", "age_cat")], by="id")

#recode vote intention
dat <- dat %>% mutate(vote_intention2 = case_when(vote_intention=="Other" ~ "Noanswer",
                                                  vote_intention=="No show" ~ "Noanswer",
                                                  vote_intention=="Don't know" ~ "Noanswer", 
                                                  TRUE ~ vote_intention))
dat$vote_intention2 <- relevel(factor(dat$vote_intention2, ordered=F), ref ="Noanswer")

#create dummies for vote intention
pty <- fastDummies::dummy_cols(dat$vote_intention2)
dat <- cbind(dat, pty)

dat <- rename(dat,
              AfD=".data_AfD",
              CDU=".data_CDU/CSU",
              FDP=".data_FDP",
              Greens=".data_Greens",                
              SPD=".data_SPD",                 
              Left=".data_The Left",
              Noanswer=".data_Noanswer")

dat$east <- relevel(factor(dat$east, ordered = F), ref = "West")

#### TABLES AND FIGURES ####--------------------------------------------------------

#Table D1: Likert-scale with East-West dummy

#interaction between East-West and age
modeast1 <- lm(border_open_close_1 ~ AfD+FDP+CDU+SPD+Left+Greens+
                #left_right_1 + immigration_general_1 + european_integration_1 +
                gender + age*east + urban_rural + educat + migration_background,
              data=dat)
summary(modeast1)

#interaction Iron curtain and age
modeast2 <- lm(border_open_close_1 ~ AfD+FDP+CDU+SPD+Left+Greens+
                 #left_right_1 + immigration_general_1 + european_integration_1 +
                 gender + age*log(distance_ironcurtain) + urban_rural + educat + migration_background,
               data=dat)
summary(modeast2)

#interaction Berlin wall and age
modeast3 <- lm(border_open_close_1 ~ AfD+FDP+CDU+SPD+Left+Greens+
                 #left_right_1 + immigration_general_1 + european_integration_1 +
                 gender + age*log(distance_berlinwall) + urban_rural + educat + migration_background,
               data=dat)
summary(modeast3)

texreg(list(modeast1,modeast2, modeast3),
       file = "tab_F1.tex",
       include.ci = F,
       digits = 3,
       single.row = TRUE,
       custom.header = list("Preference for closed border (0-10)" = 1:3))

#Figure F1: Marginal means
modeast4 <- lm(border_open_close_1 ~ AfD+FDP+CDU+SPD+Left+Greens+
                 #left_right_1 + immigration_general_1 + european_integration_1 +
                 gender + age*distance_berlinwall + urban_rural + educat + migration_background,
               data=dat)
wallint <- plot_model(modeast4, terms=c("distance_berlinwall", "age"), type="int", mdrt.values="meansd")

pdf(file = "fig_F1.pdf",width = 8,height = 5,onefile=F)

wallint +  
  scale_color_viridis_d(name="Berlin Wall", labels=c("~200(mean-sd)", "~350 (mean)", "~500 (mean+sd)"))+
  scale_fill_viridis_d(guide=none) +
  labs(x="Age in years", y="Predicted values", title="") +
  theme_minimal() +
  theme(
    axis.text.y.left = element_text(size = 12),
    axis.text.x = element_text(size=12),
    axis.title.y= element_text(size = 12),
    axis.title.x = element_text(size=12),
    legend.text = element_text(size=12),
    legend.title = element_text(size=12),
    legend.position = "top")

dev.off()

#Figure F2: Conjoint by East-West-Berlin
dat_east <- dat_long %>%
  left_join(dat[,c("id", "east")], by="id")

dat_east$east <- as.factor(dat_east$east)

results_east <- cregg::cj(chosen ~ border+aim+country, data=dat_east,
                          id=~id, estimate="mm", h0=0.5, by=~east)

#make labels for plots
labels<- as_labeller(c("border" = "Measure", "aim"="Aim", "country"="Country", "sim_cat" = "Country"))

pdf(file = "fig_F2.pdf",width = 8,height = 5,onefile=F)

ggplot(results_east)+
  geom_point(aes(x=estimate, y=level, color=east), size=2, position = position_dodge(0.4))+
  geom_errorbar(aes(xmin=lower, xmax=upper, y=level, color=east), width=0, size=1, position = position_dodge(0.4))+
  facet_grid(feature~., scales = "free", labeller = labels)+
  geom_vline(xintercept = 0.5, linetype="dotted")+
  labs(x="Marginal Mean", y="", color="")+
  scale_color_manual(values=c("#3b528b", "#5ec962", "#fde725"))+
  theme_minimal() +
  theme(
    axis.text.y.left = element_text(size = 16),
    axis.text.x = element_text(size=16),
    axis.title.y= element_text(size = 16),
    axis.title.x = element_text(size=16),
    strip.text.y.right = element_text(size=16),
    legend.text = element_text(size=16),
    legend.position = "top")

dev.off()

#Figure F3: by iron curtain
results_iron <- cregg::cj(chosen ~ border+aim+country, data=dat_long,
                          id=~id, estimate="mm", h0=0.5, by=~ironcurtaindist)

pdf(file = "fig_F3.pdf",width = 8,height = 5,onefile=F)

ggplot(results_iron)+
  geom_point(aes(x=estimate, y=level, color=ironcurtaindist), size=2, position = position_dodge(0.4))+
  geom_errorbar(aes(xmin=lower, xmax=upper, y=level, color=ironcurtaindist), width=0, size=1, position = position_dodge(0.4))+
  facet_grid(feature~., scales = "free", labeller = labels)+
  geom_vline(xintercept = 0.5, linetype="dotted")+
  labs(x="Marginal Mean", y="", color="Iron Curtain")+
  scale_color_viridis(discrete=T)+
  theme_minimal() +
  theme(
    axis.text.y.left = element_text(size = 16),
    axis.text.x = element_text(size=16),
    axis.title.y= element_text(size = 16),
    axis.title.x = element_text(size=16),
    strip.text.y.right = element_text(size=16),
    legend.text = element_text(size=16),
    legend.title = element_text(size=16),
    legend.position = "top")

dev.off()

#Figure F4: by age and berlin wall
#people living close
dat_wall <- dat_long %>% filter(distance_berlinwall<=200)

results_wall <- cregg::cj(chosen ~ border+aim+country, data=dat_wall,
                          id=~id, estimate="mm", h0=0.5, by=~age_cat)

w1 <- ggplot(results_wall)+
  geom_point(aes(x=estimate, y=level, color=age_cat), size=2, position = position_dodge(0.5))+
  geom_errorbar(aes(xmin=lower, xmax=upper, y=level, color=age_cat), width=0, size=1, position = position_dodge(0.5))+
  facet_grid(feature~., scales = "free", labeller = labels)+
  geom_vline(xintercept = 0.5, linetype="dotted")+
  labs(x="Marginal Mean", y="", color="Age", title="People living within 200km")+
  scale_color_viridis(discrete=T)+
  theme_minimal() +
  theme(
    axis.text.y.left = element_text(size = 16),
    axis.text.x = element_text(size=16),
    axis.title.y= element_text(size = 16),
    axis.title.x = element_text(size=16),
    strip.text.y.right = element_text(size=16),
    legend.text = element_text(size=16),
    legend.title = element_text(size=16),
    legend.position = "top")

#people living far away
dat_wall2 <- dat_long %>% filter(distance_berlinwall>500)

results_wall2 <- cregg::cj(chosen ~ border+aim+country, data=dat_wall2,
                          id=~id, estimate="mm", h0=0.5, by=~age_cat)

w2 <- ggplot(results_wall2)+
  geom_point(aes(x=estimate, y=level, color=age_cat), size=2, position = position_dodge(0.5))+
  geom_errorbar(aes(xmin=lower, xmax=upper, y=level, color=age_cat), width=0, size=1, position = position_dodge(0.5))+
  facet_grid(feature~., scales = "free", labeller = labels)+
  geom_vline(xintercept = 0.5, linetype="dotted")+
  labs(x="Marginal Mean", y="", color="Age", title="People living >500km away")+
  scale_color_viridis(discrete=T)+
  theme_minimal() +
  theme(
    axis.text.y.left = element_text(size = 16),
    axis.text.x = element_text(size=16),
    axis.title.y= element_text(size = 16),
    axis.title.x = element_text(size=16),
    strip.text.y.right = element_text(size=16),
    legend.text = element_text(size=16),
    legend.title = element_text(size=16),
    legend.position = "top")

pdf(file = "fig_F4.pdf",width = 10,height = 6,onefile=F)
ggarrange(w1, w2, ncol=2, common.legend=T)
dev.off()
